package com.vinfai.common.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vinfai.common.dao.IEntityDAO;
import com.vinfai.common.exception.AppException;
import com.vinfai.common.exception.DataAccessException;
import com.vinfai.common.service.IBaseService;
@Service
public abstract class BaseService<E extends Serializable,PK extends Serializable> implements IBaseService<E,PK> {
	
	protected  IEntityDAO<E,PK> entityDAO;
	
	public abstract void setEntityDAO(IEntityDAO<E,PK> entityDAO);
	
	
	public int bulkNativeUpdate(String nativeSql) throws AppException {
		try {
			 return entityDAO.bulkNativeUpdate(nativeSql);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public int bulkNativeUpdate(String nativeSql, Map<String,?> params)
			throws AppException {
		try {
			 return entityDAO.bulkNativeUpdate(nativeSql, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public int bulkNativeUpdate(String nativeSql, Object... params)
			throws AppException {
		try {
			 return entityDAO.bulkNativeUpdate(nativeSql, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public int bulkUpdate(String updateSql, Map<String,?> params) throws AppException {
		try {
			 return entityDAO.bulkUpdate(updateSql,params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public int bulkUpdate(String updateSql) throws AppException {
		try {
			 return entityDAO.bulkUpdate(updateSql);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public int bulkUpdate(String updateSql, Object... params)
			throws AppException {
		try {
			 return entityDAO.bulkUpdate(updateSql,params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public void delete(E entity) throws AppException {
		try {
			entityDAO.delete(entity);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
		
	}

	public void deleteById(PK id) throws AppException {
		try {
			entityDAO.deleteById(id);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public <T> List<T> find(String hql, Map<String,?> params) throws AppException {
		try {
			return entityDAO.find(hql, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public <T> List<T> find(String hql, Object... params) throws AppException {
		try {
			return entityDAO.find(hql, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public <T> List<T>  find(String hql, int firstResult, int maxResults, Map<String,?> params)
			throws AppException {
		try {
			return entityDAO.find(hql, firstResult, maxResults, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public  <T> List<T>  find(String hql, int firstResult, int maxResults,
			Object... params) throws AppException {
		try {
			return entityDAO.find(hql, firstResult, maxResults, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public  <T> List<T>  findNativeQuery(String nativeSql, int firstResult,
			int maxResults, Object... params) throws AppException {
		try {
			return entityDAO.findNativeQuery(nativeSql, firstResult, maxResults, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public  <T> List<T>  findNativeQuery(String nativeSql, int firstResult,
			int maxResults, Map<String,?> params) throws AppException {
		try {
			return entityDAO.findNativeQuery(nativeSql, firstResult, maxResults, params);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public E getById(PK id) throws AppException {
		try {
			return entityDAO.getById(id);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public void merge(E entity) throws AppException {
		try {
			entityDAO.merge(entity);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public PK save(E entity) throws AppException {
		try {
			return entityDAO.save(entity);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public void saveOrUpdate(E entity) throws AppException {
		try {
			entityDAO.saveOrUpdate(entity);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
	}

	public void update(E entity) throws AppException {
		try {
			entityDAO.save(entity);
		} catch (DataAccessException e) {
			throw new AppException("", e);
		}
		
	}
	
}
